package com.yizhuoyan.his.webapi.controller.pharmacyoperator;

import com.yizhuoyan.his.ao.CaseRegistrationRecordAo;
import com.yizhuoyan.his.dto.JSONResponse;
import com.yizhuoyan.his.entity.CaseRegistrationRecordEntity;
import com.yizhuoyan.his.entity.DocPrescriptionDetailEntity;
import com.yizhuoyan.his.entity.DocPrescriptionEntity;
import com.yizhuoyan.his.entity.HosDrugsEntity;
import com.yizhuoyan.his.qco.CaseRegistrationRecordQco;
import com.yizhuoyan.his.service.doctor.outpatient.PrescriptionService;
import com.yizhuoyan.his.service.pharmacyoperator.DrugManageService;
import com.yizhuoyan.his.service.pharmacyoperator.DrugSendBackService;
import com.yizhuoyan.his.webapi.controller.pharmacyoperator.vo.CaseRegistrationRecordVo;
import com.yizhuoyan.his.webapi.controller.pharmacyoperator.vo.DrugSendBackVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
public class DrugSendBackController {
    @Autowired
    DrugSendBackService service;
    @Autowired
    DrugManageService   dService;

    @GetMapping("/queryCaseRegistrationRecord")
    public JSONResponse queryCaseRegistrationRecord (CaseRegistrationRecordAo ao) throws Exception{
        CaseRegistrationRecordQco qco = CaseRegistrationRecordQco.of(ao);
        List<CaseRegistrationRecordEntity> caseRegistrationRecordEntities = service.queryCaseRegistrationRecord(qco);
        List<CaseRegistrationRecordVo> list = new ArrayList<>(caseRegistrationRecordEntities.size());
        for(CaseRegistrationRecordEntity entity :caseRegistrationRecordEntities){
            CaseRegistrationRecordVo vo = CaseRegistrationRecordVo.of(entity);
            list.add(vo);
        }
        return JSONResponse.ok(list);
    }
    @GetMapping("/queryDocPrescriptionDetail/{id}")
    public JSONResponse queryDocPrescriptionDetail (@PathVariable Long id) throws Exception{

        List<DocPrescriptionDetailEntity> docPrescriptionDetailEntities = service.queryDocPrescriptionDetail(id);
        List<DrugSendBackVo> list = new ArrayList<>(docPrescriptionDetailEntities.size());
        double money = service.queryMoney(id);
        List<DocPrescriptionEntity> docPrescriptionEntities = service.queryPrescribeDoctor(id);
        for(DocPrescriptionDetailEntity entity:docPrescriptionDetailEntities){
            Long drugsId = entity.getDrugsId();
            HosDrugsEntity hosDrugsEntity = dService.queryDrug(drugsId);
            System.out.println(hosDrugsEntity);
            DrugSendBackVo of = DrugSendBackVo.of(hosDrugsEntity, entity, money, docPrescriptionEntities);
            DrugSendBackVo vo = new DrugSendBackVo();
            list.add(of);
        }
        return JSONResponse.ok(list);
    }
    @PutMapping("/modifySendBackDrugCount/{id}")
    public JSONResponse modifySendBackDrugCount(@PathVariable Long id,int count) throws Exception{
        service.modifySendBackDrugCount(id,count);
        return JSONResponse.ok();
    }
    @GetMapping("/queryMoney/{id}")
    public JSONResponse queryMoney(@PathVariable Long id) throws Exception{
        double money = service.queryMoney(id);
        return JSONResponse.ok(money);
    }

}
